// 获取project基本信息
export const fetchProject = async (data: {
    baseUrl?: string
    token?: string
    projectId?: string
}) => {
    const { baseUrl, token, projectId } = data
    if (!baseUrl || !projectId) {
        return null
    }
    let url = `${baseUrl}/api/project/get?id=${projectId}`
    if (token) {
        url += `&token=${token}`
    }
    const res = await fetch(url)
    const json = await res.json()
    return json?.data || null
}

// 调用yapi的cat_list接口
export const fetchCatList = async (data: {
    baseUrl?: string
    token?: string
    catId?: string
}) => {
    const { baseUrl, catId, token } = data
    if (!catId || !baseUrl) {
        return []
    }
    let url = `${baseUrl}/api/interface/list_cat?page=1&limit=100&catid=${catId}`
    if (token) {
        url += `&token=${token}`
    }
    const res = await fetch(url)
    const json = await res.json()
    return json?.data?.list || []
}

// 获取某个接口的数据
export const fetchApiData = async (data: {
    baseUrl?: string
    token?: string
    apiId?: string
}) => {
    const { baseUrl, apiId, token } = data
    if (!apiId || !baseUrl) {
        return null
    }
    let url = `${baseUrl}/api/interface/get?id=${apiId}`
    if (token) {
        url += `&token=${token}`
    }
    const res = await fetch(url)
    const json = await res.json()
    return json?.data || null
}

// 批量请求接口数据
export const fetchApiDataBatch = async (data: {
    baseUrl?: string
    token?: string
    apiIds?: string[]
}) => {
    const { baseUrl, token, apiIds } = data
    if (!apiIds?.length || !baseUrl) {
        return []
    }
    const promises = apiIds.map((apiId) =>
        fetchApiData({ baseUrl, token, apiId }),
    )
    const results = await Promise.all(promises)
    return results
}
